library(effsize)
library(esc)

### DATA SET UP ###

realdata <- read.csv(file.choose(),header = TRUE)

realdata$group1_n <- as.numeric(realdata$group1_n)
realdata$group1_mean <- as.numeric(realdata$group1_mean)
realdata$group1_sd <- as.numeric(realdata$group1_sd)
realdata$group2_n <- as.numeric(realdata$group2_n)
realdata$group2_mean <- as.numeric(realdata$group2_mean)
realdata$group2_sd <- as.numeric(realdata$group2_sd)
realdata$n_total <- as.numeric(realdata$n_total)

realdata$t_test_value <- as.numeric(realdata$t_test_value)
realdata$F_test_value <- as.numeric(realdata$F_test_value)
realdata$data_type <- as.numeric(realdata$data_type)

realdata$design <- as.factor(realdata$design)
realdata$thinking <- as.factor(realdata$thinking)
realdata$location <- as.factor(realdata$location)
realdata$measure <- as.factor(realdata$measure)
realdata$random <- as.factor(realdata$random)



### COHEN'S D CALCULATIONS ###

es_values <- numeric(nrow(realdata))
se_values <- numeric(nrow(realdata))
var_values <- numeric(nrow(realdata))

# Calculations depending on data type
for (i in 1:nrow(realdata)) {
  type <- realdata$data_type[i]
  
  if (type == 1) {    # mean/sd/n
    result <- esc_mean_sd(
      grp1n = realdata$group2_n[i], 
      grp1m = realdata$group2_mean[i], 
      grp1sd = realdata$group2_sd[i], 
      grp2n = realdata$group1_n[i], 
      grp2m = realdata$group1_mean[i], 
      grp2sd = realdata$group1_sd[i], 
      es.type = "d"
    )
    es_values[i] <- result$es
    se_values[i] <- result$se
    var_values[i] <- result$var
  } else if (type == 2) {    # t tests
    result <- esc_t(
      t = realdata$t_test_value[i],
      grp1n = realdata$group1_n[i],
      grp2n = realdata$group2_n[i],
      es.type = "d"
    )
    es_values[i] <- result$es
    se_values[i] <- result$se
    var_values[i] <- result$var
  } else if (type == 3) {   # F tests
    result <- esc_f(
      f = realdata$F_test_value[i],
      totaln = realdata$n_total[i],
      es.type = "d"
    )
    es_values[i] <- result$es
    se_values[i] <- result$se
    var_values[i] <- result$var
  } else if (type == 4) {   # cohen's D
    es_values[i] <- realdata$d_es[i]  # Leave as is
    se_values[i] <- NA
    var_values[i] <- NA
  }
}

realdata$d_es <- es_values
realdata$d_se <- se_values
realdata$d_var <- var_values


### META ANALYSIS ###
library(meta)

divergent_data <- realdata[realdata$thinking == 1, ]
convergent_data <- realdata[realdata$thinking == 2, ]

MA_divergent_design <- metagen(TE = d_es, 
                         seTE = d_se, 
                         studlab = study, 
                         data = divergent_data,
                         sm = "SMD",
                         method.smd = "Cohen",
                         random = TRUE,
                         common = FALSE,
                         method.tau = "DL",
                         subgroup = design
                         )

# 1100 x 800
forest(MA_divergent_design,
       leftcols = c("studlab", "n_total", "TE", "seTE"),
       leftlabs = c("Study", "Total N", "Cohen's D", "SE"),
       rightlabs = c("Cohen's D", "95% CI", "Weight"),
       smlab = "Effect Size",
       sortvar = study,
       label.left = "Favours Control",
       label.right = "Favours Walking",
       xlim = c(-4,4),
       subgroup.name = "Study Design"
       )



MA_convergent_design <- metagen(TE = d_es, 
                            seTE = d_se, 
                            studlab = study, 
                            data = convergent_data,
                            sm = "SMD",
                            method.smd = "Cohen",
                            random = TRUE,
                            common = FALSE,
                            method.tau = "DL",
)

# 1100 x 400
forest(MA_convergent_design,
       leftcols = c("studlab", "n_total", "TE", "seTE"),
       leftlabs = c("Study", "Total N", "Cohen's D", "SE"),
       rightlabs = c("Cohen's D", "95% CI", "Weight"),
       smlab = "Effect Size",
       sortvar = study,
       label.left = "Favours Control",
       label.right = "Favours Walking",
       xlim = c(-3,3),
       subgroup.name = "Study Design"
)


MA_new_div_location <- metagen(TE = d_es, 
                            seTE = d_se, 
                            studlab = study, 
                            data = divergent_data[divergent_data$location %in% c("Indoor", "Outdoor"), ],
                            sm = "SMD",
                            method.smd = "Cohen",
                            random = TRUE,
                            common = FALSE,
                            method.tau = "DL",
                            subgroup = location
)

# 1100 x 800
forest(MA_new_div_location,
       leftcols = c("studlab", "n_total", "TE", "seTE"),
       leftlabs = c("Study", "Total N", "Cohen's D", "SE"),
       rightlabs = c("Cohen's D", "95% CI"),
       smlab = "Effect Size",
       sortvar = study,
       label.left = "Favours Control",
       label.right = "Favours Walking",
       xlim = c(-4,4),
       subgroup.name = "Walking Location",
       comb.subgroup = FALSE,
       overall = FALSE,
       overall.hetstat = FALSE 
)

MA_new_conv_location <- metagen(TE = d_es, 
                               seTE = d_se, 
                               studlab = study, 
                               data = convergent_data,
                               sm = "SMD",
                               method.smd = "Cohen",
                               random = TRUE,
                               common = FALSE,
                               method.tau = "DL",
                               subgroup = location
)


# 1100 x 400
forest(MA_new_conv_location,
       leftcols = c("studlab", "n_total", "TE", "seTE"),
       leftlabs = c("Study", "Total N", "Cohen's D", "SE"),
       rightlabs = c("Cohen's D", "95% CI"),
       smlab = "Effect Size",
       sortvar = study,
       label.left = "Favours Control",
       label.right = "Favours Walking",
       xlim = c(-3,3),
       subgroup.name = "Walking Location",
       comb.subgroup = FALSE,
       overall = FALSE,
       overall.hetstat = FALSE 
)



MA_new_div_measure <- metagen(TE = d_es, 
                               seTE = d_se, 
                               studlab = study, 
                               data = divergent_data,
                               sm = "SMD",
                               method.smd = "Cohen",
                               random = TRUE,
                               common = FALSE,
                               method.tau = "DL",
                               subgroup = measure
)


# 1100 x 800
forest(MA_new_div_measure,
       leftcols = c("studlab", "n_total", "TE", "seTE"),
       leftlabs = c("Study", "Total N", "Cohen's D", "SE"),
       rightlabs = c("Cohen's D", "95% CI"),
       smlab = "Effect Size",
       sortvar = study,
       label.left = "Favours Control",
       label.right = "Favours Walking",
       xlim = c(-4,4),
       subgroup.name = "Creativity Measure",
       comb.subgroup = FALSE,
       overall = FALSE,
       overall.hetstat = FALSE 
)


MA_new_conv_measure <- metagen(TE = d_es, 
                                seTE = d_se, 
                                studlab = study, 
                                data = convergent_data,
                                sm = "SMD",
                                method.smd = "Cohen",
                                random = TRUE,
                                common = FALSE,
                                method.tau = "DL",
                                subgroup = measure
)


# 1100 x 400
forest(MA_new_conv_measure,
       leftcols = c("studlab", "n_total", "TE", "seTE"),
       leftlabs = c("Study", "Total N", "Cohen's D", "SE"),
       rightlabs = c("Cohen's D", "95% CI"),
       smlab = "Effect Size",
       sortvar = study,
       label.left = "Favours Control",
       label.right = "Favours Walking",
       xlim = c(-3,3),
       subgroup.name = "Creativity Measure",
       comb.subgroup = FALSE,
       overall = FALSE,
       overall.hetstat = FALSE 
)




MA_new_div_random <- metagen(TE = d_es, 
                               seTE = d_se, 
                               studlab = study, 
                               data = divergent_data[divergent_data$random == 1, ],
                               sm = "SMD",
                               method.smd = "Cohen",
                               random = TRUE,
                               common = FALSE,
                               method.tau = "DL",
                               subgroup = design
)

# 1100 x 800
forest(MA_new_div_random,
       leftcols = c("studlab", "n_total", "TE", "seTE"),
       leftlabs = c("Study", "Total N", "Cohen's D", "SE"),
       rightlabs = c("Cohen's D", "95% CI"),
       smlab = "Effect Size",
       sortvar = study,
       label.left = "Favours Control",
       label.right = "Favours Walking",
       xlim = c(-4,4),
       subgroup.name = "Study Design"
)



### FUNNEL PLOTS ###


MA_divergent <- metagen(TE = d_es, 
                        seTE = d_se, 
                        studlab = study, 
                        data = divergent_data,
                        sm = "SMD",
                        method.smd = "Cohen",
                        random = TRUE,
                        common = FALSE,
                        method.tau = "DL"
)

funnel(MA_divergent)

metabias(MA_divergent, method.bias = "linreg")


MA_convergent <- metagen(TE = d_es, 
                        seTE = d_se, 
                        studlab = study, 
                        data = convergent_data,
                        sm = "SMD",
                        method.smd = "Cohen",
                        random = TRUE,
                        common = FALSE,
                        method.tau = "DL"
)

funnel(MA_convergent)

metabias(MA_convergent, method.bias = "linreg")
